import React, {Component} from 'react';
import {Redirect, Route} from "react-router-dom";
import {hasToken} from "../utils/local";

class AuthRoute extends Component {
  render() {
    const {component: Com, ...rest} = this.props

    // layout
    // 权限判断
    // 如果有权限再跳转
    // 没有权限 ==> 去登录界面c
    // 路由的render的写法

    return <Route
      {...rest}
      render={(props) => {
        // 动态渲染
        // 组件内部获取不到路由信息
        if (!hasToken()) return <Redirect
          to={{
            pathname: '/login',
            state: {
              fromPath: this.props.location.pathname
            }
          }}
        />
        return <Com {...props}/>
      }}
    />
  }
}

export default AuthRoute;
